package com.woniuxy.caradmin.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.caradmin.entity.pojo.Vehiclefiles;
import com.woniuxy.caradmin.entity.pojo.Yearcheckrecord;
import com.woniuxy.caradmin.entity.vo.YearcheckrecordVo;
import com.woniuxy.caradmin.exception.CarException;
import com.woniuxy.caradmin.service.TroublerecordService;
import com.woniuxy.caradmin.service.VehiclefilesService;
import com.woniuxy.caradmin.service.YearcheckrecordService;
import com.woniuxy.caradmin.util.Constaint;
import com.woniuxy.caradmin.util.DataGridView;
import com.woniuxy.caradmin.util.R;
import com.woniuxy.caradmin.util.ResultCode;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author womenzu
 * @since 2020-12-07
 */
@RestController
@RequestMapping("/yearcheckrecord")
public class YearcheckrecordController {
    @Autowired
    private YearcheckrecordService yearcheckrecordService;
    @Autowired
    private VehiclefilesService vehiclefilesService;
    @GetMapping("/yearcheckrecordById")
    @ApiOperation(value = "id查询年检记录")
    public R yearcheckrecordById(Integer id) {

        Yearcheckrecord yearcheckrecord = yearcheckrecordService.selectYearcheckrecordByid(id);
        if (yearcheckrecord !=null) {
            Date time  = yearcheckrecord.getTime();
            Date nexttime  = yearcheckrecord.getNexttime();
            java.sql.Date timesql=new java.sql.Date(time.getTime());
            java.sql.Date nexttimesql=new java.sql.Date(nexttime.getTime());
            yearcheckrecord.setTime(timesql);
            yearcheckrecord.setNexttime(nexttimesql);
            return R.ok().data("yearcheckrecord",yearcheckrecord);
        } else {
            throw new CarException(ResultCode.YEARISNULL, Constaint.YEARISNULL);
        }
    }

    @GetMapping("/yearcheckrecordList")
    @ApiOperation(value = "查询年检记录")
    public DataGridView yearcheckrecordList(YearcheckrecordVo yearcheckrecordVo) {
        Page<Object> page = PageHelper.startPage(yearcheckrecordVo.getPage(), yearcheckrecordVo.getLimit());
        List<Yearcheckrecord> yearcheckrecords = yearcheckrecordService.selectAllByPage(yearcheckrecordVo);
        if (yearcheckrecords != null && yearcheckrecords.size() > 0) {
            return new DataGridView(page.getTotal(), yearcheckrecords);
        } else {
            throw new CarException(ResultCode.YEARISNULL, Constaint.YEARISNULL);
        }
    }
    @GetMapping("/yearcheckrecordList1")
    @ApiOperation(value = "查询年检记录")
    public R yearcheckrecordList1() {

        List<Yearcheckrecord> yearcheckrecords =yearcheckrecordService.selectYearcheckrecordALl();
        if (yearcheckrecords != null && yearcheckrecords.size() > 0) {
           return R.ok().data("yearcheckrecords",yearcheckrecords);
        } else {
            throw new CarException(ResultCode.YEARISNULL, Constaint.YEARISNULL);
        }
    }

    @GetMapping("/addyearcheckrecord")
    @ApiOperation(value = "提交年检记录")
    public R addyearcheckrecord(Yearcheckrecord yearcheckrecord, HttpServletRequest httpServletRequest) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String vid = httpServletRequest.getParameter("vid");
        String time = httpServletRequest.getParameter("time");
        System.out.println(vid);
        String nexttime = httpServletRequest.getParameter("nexttime");
        yearcheckrecord.setVid(Integer.parseInt(vid));
        java.sql.Date et = null;
        java.sql.Date st = null;

        try {
           Date  st1 = sdf.parse(time);
              st = new java.sql.Date(st1.getTime());
            Date et1 = sdf.parse(nexttime);
            et = new java.sql.Date(et1.getTime());
        } catch (ParseException e) {
            yearcheckrecord.setTime(st);
        }
        Vehiclefiles vehiclefiles = vehiclefilesService.selecctVehicleById(Integer.parseInt(vid));
        System.out.println(vehiclefiles.getType().getId());
        Date f1 =new Date();

        Date purchasedate = vehiclefiles.getPurchasedate();
       if(vehiclefiles.getType().getId()==4){
                if (f1.getTime()-purchasedate.getTime()<6*365* 24 * 3600 * 1000){
                    System.out.println(f1.getTime()-purchasedate.getTime());
                    System.out.println(6*365* 24 * 3600 * 1000);
                    if((et.getTime()-st.getTime())/24 /3600 / 1000<21*30||(et.getTime()-st.getTime())/24 / 3600 /1000>2*365 ){

                     throw new CarException(ResultCode.SSNTYEARCHECK,Constaint.SSNTYEARCHECK);
                    }else {
                      yearcheckrecord.setNexttime(et);
                    }
                }else if(f1.getTime()-purchasedate.getTime()>6*365* 24 * 3600 * 1000&&(f1.getTime()-purchasedate.getTime())/24 / 3600 /1000<15*365){
                    if((et.getTime()-st.getTime())/24 /3600 / 1000<9*30 ||(et.getTime()-st.getTime())/24 / 3600 / 1000>1*365){
                        throw  new CarException(ResultCode.SNTYEARCHECK,Constaint.SNTYEARCHECK);
                    }else {
                      yearcheckrecord.setNexttime(et);
                    }
                }else {
                    if((et.getTime()-st.getTime())/24 /3600 / 1000<3*30||(et.getTime()-st.getTime())/24 /3600 / 1000>6*30){
                        throw  new CarException(ResultCode.FSNTYEARCHECK,Constaint.FSNTYEARCHECK);
                    }else {
                        yearcheckrecord.setNexttime(et);
                    }
                }}

            else {
                if((f1.getTime()-purchasedate.getTime())/24 / 3600 /1000<10*365){
                    if((et.getTime()-st.getTime())/24 /3600 / 1000<9*30||(et.getTime()-st.getTime())/24 /3600 / 1000>1*365){
                        throw new CarException(ResultCode.LMNTYEARCHECK,Constaint.LMNTYEARCHECK);
                    }else {
                       yearcheckrecord.setNexttime(et);
                    }
                }
                if((f1.getTime()-purchasedate.getTime())/24 / 3600 /1000>10*365){
                    if((et.getTime()-st.getTime())/24 /3600 / 1000<3*30||(et.getTime()-st.getTime())/24 /3600 / 1000>6*30){
                        throw new CarException(ResultCode.TLMNTYEARCHECK,Constaint.TLMNTYEARCHECK);
                    }else {
                      yearcheckrecord.setNexttime(et);
                    }
            }
        }
         yearcheckrecord.setStatus(1);
        int i = yearcheckrecordService.addYearcheckrecord(yearcheckrecord);
        if (i > 0) {
            return R.ok().code(ResultCode.ADDYEARSUC).message(Constaint.ADDYEARSUC);
        } else {
            throw new CarException(ResultCode.ADDYEARFAIL, Constaint.ADDYEARFAIL);

        }

    }

    @GetMapping("/upyearcheckrecord")
    @ApiOperation(value = "修正年检记录")
    public R upyearcheckrecord(Yearcheckrecord yearcheckrecord) {

        int i = yearcheckrecordService.upYearcheckrecord(yearcheckrecord);
        if (i > 0) {
            return R.ok().code(ResultCode.UPYEARSUC).message(Constaint.UPYEARSUC);
        } else {
            throw new CarException(ResultCode.UPYEARFAIL, Constaint.UPYEARFAIL);
        }
    }

    @GetMapping("/removeyearcheckrecord")
    @ApiOperation(value = "删除年检记录")
    public R removeyearcheckrecord(Integer[] ids) {

        boolean b = yearcheckrecordService.removeYearcheckrecord(ids);
        if (b) {
            return R.ok().code(ResultCode.REMOVEYEARSUC).message(Constaint.REMOVEYEARSUC);
        } else {
            throw new CarException(ResultCode.REMOVEYEARFAIL, Constaint.REMOVEYEARFAIL);
        }
    }
}

